package org.example.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.flow.domain.Business;
import org.example.flow.domain.Flow;
import org.example.flow.domain.Shop;
import org.example.flow.service.BusinessService;
import org.example.flow.service.ShopService;
import org.example.flow.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/supermarket")
@RestController
public class SupermarketControler {
    @Autowired
    ShopService shopService;
    @RequestMapping("/list")
    public Result list(Integer pageNum,Integer activeName){
        PageHelper.startPage(pageNum,5);
        LambdaQueryWrapper<Shop> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(Shop::getBusinessId,activeName);
        List<Shop> list = shopService.list(wrapper1);
        return Result.success(new PageInfo<>(list));
    }
    @RequestMapping("/buy")
    public Result buy(Integer num,Integer id){
        LambdaQueryWrapper<Shop> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Shop::getId,id);
        Shop one = shopService.getOne(wrapper);
        if(num>one.getNum()){
            return Result.fail("库存不足");
        }
        if(one.getNum()==num){
            one.setStatus("缺货中");
        }
        one.setNum(one.getNum()-num);
        boolean b = shopService.updateById(one);
        return b?Result.success():Result.fail();
    }
}
